/*
 * 人人信用管理有限公司版权所有.
 * 
 * @Description:
 * 
 * @author: liyuliang
 * 
 * @date: 20 Apr 2017 10:44:16 am
 * 
 * 注意：本内容仅限于人人信用管理有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.rrx360.common.config.global;

import java.util.ArrayList;
import java.util.List;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.rrx360.common.config.ConfigNode;
import com.rrx360.common.config.ConfigNodeGetter;
import com.rrx360.common.config.ConfigNodeImpl;

public class DefaultAppConfigNodeGetter implements ConfigNodeGetter {

  public static final DefaultAppConfigNodeGetter instance = new DefaultAppConfigNodeGetter();

  private ConfigNodeImpl configNode;

  public static final String NODE_ID = "global.app";

  public ConfigNode get() {
    if (configNode == null) {
      configNode = new ConfigNodeImpl();
      configNode.setNodeId(NODE_ID);
      List<Object> list = new ArrayList<>();
      try {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:config/app.properties");
        for (Resource resource : resources) {
          list.add(resource.getURL());
        }
        resources = resolver.getResources("classpath*:config/app-*.properties");
        for (Resource resource : resources) {
          list.add(resource.getURL());
        }
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
      configNode.setConfigFileList(list);
    }
    return configNode;
  }
}
